package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.exception.BusyException;
import com.example.demo.exception.login.WrongPasswordException;
import com.example.demo.exception.modify.UserNotExitException;
import com.example.demo.exception.regist.UserExitException;
import com.example.demo.exception.regist.UserNullException;
import com.example.demo.service.UserService;
import com.example.demo.utils.Message;
import com.jcraft.jsch.SftpException;
import org.apache.shiro.authc.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@CrossOrigin
@RestController
@RequestMapping("user")
public class UserController {
    @Resource
    UserService userService;

    /**
     * 登录
     * @param user 用户名和密码
     * @return 是否登录成功
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public Message login(@RequestBody User user) {
        String msg = "登录成功";
        try {
            userService.login(user.getUsername(), user.getPassword());
            return Message.createSuc(msg);
        } catch (DisabledAccountException e) {
            e.printStackTrace();
            e.printStackTrace();
            msg = "账号已被禁用";
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            e.printStackTrace();
            msg = "该账户不存在";
        } catch (ExcessiveAttemptsException e) {
            e.printStackTrace();
            e.printStackTrace();
            msg = "登录失败次数过多";
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            e.printStackTrace();
            msg = "用户名或密码输入错误";
        } catch (ExpiredCredentialsException e) {
            e.printStackTrace();
            msg = "密码失效";
        }
        return Message.createErr(msg);

     }

    /**
     * 修改个人信息，上传头像
     * @param nickname 昵称
     * @param sex 性别
     * @param province 省
     * @param city 市
     * @param introduction 签名
     * @param file 头像图片
     * @return
     */
    @RequestMapping(value="modify",method=RequestMethod.POST)
    public Message modify(@RequestParam(value = "nickname",required = false)String nickname,@RequestParam(value = "sex",required = false)Boolean sex,@RequestParam(value = "province",required = false)String province,@RequestParam(value = "city",required = false)String city,@RequestParam(value = "introduction",required = false)String introduction, @RequestParam(value = "file",required=false) MultipartFile file){
        String msg="修改成功";
        User user=new User();
        try{
            user.setNickname(nickname);
            user.setCity(city);
            user.setIntroduction(introduction);
            user.setProvince(province);
            user.setSex(sex);
            userService.modify(user,file);
            return Message.createSuc(msg);
        }catch(BusyException e){
            msg="系统繁忙，请稍等";
            return Message.createErr(msg);
        }catch (UserNotExitException e){
            msg="该用户不存在，无法修改信息";
            return Message.createErr(msg);
        } catch (Exception e) {
            msg="上传头像失败";
            e.printStackTrace();
            return Message.createErr(msg);
        }
    }

    /**
     * 修改密码
     * @param currentPwd 用户的原密码
     * @param newPwd  新密码
     * @return
     */
    @RequestMapping(value = "changePwd",method = RequestMethod.POST)
    public Message changePwd(@RequestParam(value = "currentPwd",required=false) String currentPwd,@RequestParam(value="newPwd",required=false) String newPwd){
        String msg="密码修改成功";
        try{
            userService.changePwd(currentPwd, newPwd);
            return Message.createSuc(msg);
        }catch(BusyException e){
            msg="系统繁忙，请稍等";
            return Message.createErr(msg);
        }catch(WrongPasswordException e){
            e.printStackTrace();
            msg="用户原密码错误，请重新操作";
            return Message.createErr(msg);
        } catch(UserNotExitException e){
            msg="用户不存在，无法修改密码";
            return Message.createErr(msg);
        }
    }

    /**
     * 获取个人信息
     * @return
     */
    @RequestMapping(value="getinfo",method=RequestMethod.GET)
    public Message getinfo(){
        User user =userService.getinfo();
        return Message.createSuc(user);
    }

    /**
     * 获取用户使用天数，笔记数，收藏数
     * @return
     */
    @RequestMapping(value = "notesNumber",method = RequestMethod.GET)
    public Message notesNumber(){
        Integer Num[]=userService.notesNumber();
        return Message.createSuc(Num);
    }

    @RequestMapping(value="register",method = RequestMethod.POST)
    public Message regist(@RequestBody User user){
        String msg="注册成功";
        try{
            userService.register(user);
            return Message.createSuc(msg);
        } catch (BusyException e) {
            msg = "系统繁忙，请稍等";
            return Message.createErr(msg);
        } catch (UserExitException e) {
            msg = "该用户已存在，请不要重复注册";
            return Message.createErr(msg);
        } catch (SftpException e) {
            e.printStackTrace();
            msg = "sftp连接失败";
            return Message.createErr(msg);
        } catch (IOException e) {
            e.printStackTrace();
            msg = "系统繁忙，请稍等";
            return Message.createErr(msg);
        }catch (UserNullException e){
            msg="用户名或密码不得为空";
            return Message.createErr(msg);
        }
    }

    @RequestMapping(value = "logout", method = RequestMethod.POST)
    public Message logout() {
        String msg = "退出成功";
        userService.logout();
        return Message.createSuc(msg);
    }

    @RequestMapping(value ="url2Base64",method = RequestMethod.POST)
    public Message url2Base64(@RequestParam(value = "url",required=false) String url) throws IOException {
        String msg;
        try{
            String Base64="";
            Base64=userService.url2Base64(url);
            return Message.createSuc(Base64);
        } catch (BusyException e) {
            msg = "用户越权";
            return Message.createErr(msg);
        }
    }

}


